package com.oceanstar.mapper;

import com.oceanstar.Pojo.ActivityInfo;
import com.oceanstar.Pojo.ActivitySignUpInfo;
import com.oceanstar.Pojo.ActivityParticipantsDTO;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface ActivityMapper {

    // 插入新活动
    @Insert("INSERT INTO activity_info (title, content, start_time, end_time, location, max_participants) " +
            "VALUES (#{title}, #{content}, #{startTime}, #{endTime}, #{location}, #{maxParticipants})")
    @Options(useGeneratedKeys = true, keyProperty = "activityId")
    void insertActivity(ActivityInfo info);

    // 查询活动列表（分页+状态筛选）
    @Select("SELECT * FROM activity_info " +
            "WHERE (#{status} IS NULL OR " +
            "       (CASE " +
            "           WHEN NOW() < start_time THEN 1 " +  // 开始时间之前 -> 进行中
            "           WHEN NOW() >= start_time THEN 2 " + // 开始时间之后 -> 已结束
            "        END) = #{status}) " +
            "ORDER BY start_time DESC " +
            "LIMIT #{offset}, #{pageSize}")
    List<ActivityInfo> selectActivityList(@Param("offset") Integer offset,
                                          @Param("pageSize") Integer pageSize,
                                          @Param("status") Integer status);

    // 统计活动总数（带状态筛选）
    @Select("SELECT COUNT(*) FROM activity_info " +
            "WHERE (#{status} IS NULL OR " +
            "       (CASE " +
            "          WHEN NOW() < start_time THEN 1 " +  // 开始时间之前 -> 进行中
            "           WHEN NOW() >= start_time THEN 2 " + // 开始时间之后 -> 已结束
            "        END) = #{status})")
    long countActivityList(@Param("status") Integer status);

    // 统计活动当前参与人数
    @Select("SELECT COUNT(*) FROM activity_signup_info WHERE activity_id = #{activityId}")
    int countCurrentParticipants(@Param("activityId") int activityId);

    // 查询活动报名人员列表（分页）
    @Select("SELECT asi.elderly_id as elderlyId " +
            "FROM activity_signup_info asi " +
            "WHERE asi.activity_id = #{activityId} " +
            "LIMIT #{offset}, #{pageSize}")
    List<ActivityParticipantsDTO> selectParticipantsList(@Param("offset") Integer offset,
                                                         @Param("pageSize") Integer pageSize,
                                                         @Param("activityId") int activityId);

    // 查询是否已报名
    @Select("SELECT * FROM activity_signup_info " +
            "WHERE activity_id = #{activityId} AND elderly_id = #{elderlyId}")
    ActivitySignUpInfo selectSignUpInfo(ActivitySignUpInfo info);

    // 插入报名信息
    @Insert("INSERT INTO activity_signup_info (activity_id, elderly_id) " +
            "VALUES (#{activityId}, #{elderlyId})")
    void insertSignUpInfo(ActivitySignUpInfo info);

    // 取消报名
    @Delete("DELETE FROM activity_signup_info " +
            "WHERE activity_id = #{activityId} AND elderly_id = #{elderlyId}")
    void activityCancel(ActivitySignUpInfo info);

    //根据id查询活动
    @Select("SELECT * " +
            "FROM activity_info " +
            "WHERE activity_id = #{activityId}")
    ActivityInfo selectActivityById(@Param("activityId") Integer activityId);
}